﻿dynamic class VistaEFC2{
	public static var VM_ONFOUCES:String="onFouces";
	public static var VM_ONLOSTFOUCES:String="onLostFouces";


	private var $parent:MovieClip;
	private var $EFfoot:MovieClip;
	private var $EFname:String;
	private var $EFroot:MovieClip;
	private var $mcs:Array;
	private var $centerMc:MovieClip;
	
	private var $xoffsetN:Number;
	private var $centerX:Number;
	private var $centerY:Number;
	private var $step:Number;
	private var $minScale:Number;
	private var $maxScale:Number;
	private var $minAlpha:Number;
	private var $maxAlpha:Number;
	private var $scaleLine:Number;
	private var $blance:Number;

	
	public function get Offset():Number{
		return this.$xoffsetN;
	}
	public function set Offset(value:Number){
		this.$offsetN=value;
	}
	
	public function get CenterX():Number{
		return this.$centerX;
	}
	public function set CenterX(value:Number){
		this.$centerX=value;
	}
	
	
	public function get CenterY():Number{
		return this.$centerY;
	}
	public function set CenterY(value:Number){
		this.$centerY=value;
	}
	
	
	public function get Step():Number{
		return this.$step;
	}
	public function set Step(value:Number){
		if(value>0){
			this.$step=value;
		}
	}
	
	public function get Blance():Number{
		return this.$blance;
	}
	public function set Blance(value:Number){
		this.$blance=value;
	}
	
	public function get MinScale():Number{
		return this.$minScale;
	}
	public function set MinScale(value:Number){
		this.$minScale=value;
	}
	
	public function get MaxScale():Number{
		return this.$maxScale;
	}
	public function set MaxScale(value:Number){
		this.$maxScale=value;
	}
	
	public function get MinAlpha():Number{
		return this.$minAlpha;
	}
	public function set MinAlpha(value:Number){
		this.$minAlpha=value;
	}
	
	public function get MaxAlpha():Number{
		return this.$maxAlpha;
	}
	public function set MaxAlpha(value:Number){
		this.$maxAlpha=value;
	}
	
	public function get ScaleLine():Number{
		return this.$scaleLine;
	}
	public function set ScaleLine(value:Number){
		this.$scaleLine=value;
	}
	//////////////////////////////////////////////////////////////////
	public function VistaEFC2(_mc:MovieClip){
		this.$parent=_mc;
		this.$EFroot=this.getEffectMc(this.$parent);
		initVars();
		initBroadcaster();
	}
	private function initBroadcaster(){
		AsBroadcaster.initialize(this);
	}
	public function SetMcs(mcarr:Array){
		if(mcarr.length<3){
			Say("The count of mcs can't less then 3.");
			return ;
		}
		this.$mcs=mcarr;
		this.$centerMc=this.$mcs[int(this.$mcs.length/2)];
		//initMCEvents();
	}
	
	private function initVars(){
		this.$xoffsetN=30;
		this.$centerX=0;
		this.$centerY=0;
		this.$step=5;
		this.$scaleLine=200;
		this.$minScale=50;
		this.$maxScale=120;
		this.$minAlpha=80;
		this.$maxAlpha=100;
		this.$blance=3;
		
	}
	private function initMCEvents(){
		for(var i=0;i<this.$mcs.length;i++){
			this.$mcs[i].root=this;
			this.$mcs[i].onPress=function(){
				this["root"].setCenterMc(this);
				//this["root"].$centerMc=this;
			}
		}
	}
	public function start(){
		this.$EFroot.root=this;
		this.$EFroot.onEnterFrame=this.controlFun;
		trace(this.$EFroot)
	}
	
	public function stop(){
		delete this.$EFroot.onEnterFrame;
	}
	
	private function checkAndSetMcArr(){
		var index:Number=this.findIndex(this.$centerMc);
		var ctL:Number=int(this.$mcs.length/2);
			while(index>ctL){
				this.$mcs.push(this.$mcs.shift());
				index=this.findIndex(this.$centerMc);
				
			}
		
			while(index<ctL){
				this.$mcs.unshift(this.$mcs.pop());
				index=this.findIndex(this.$centerMc);
				
			}
	}
	private function setCenterMc(mc:MovieClip){
		this.broadcastMessage(VistaEFC2.VM_ONLOSTFOUCES,this.$centerMc);
		this.$centerMc=mc;
	}
	public function setCenterMovieClip(mc:MovieClip){
		for(var i=0;i<this.$mcs.length;i++){
			if(this.$mcs[i]==mc){
				this.setCenterMc(mc);
				return;
			}
		}
	}
	public function nextone(){
		var index:Number=this.findIndex(this.$centerMc);
		this.setCenterMc(this.$mcs[index+1]);
	}
	
	public function preone(){
		var index:Number=this.findIndex(this.$centerMc);
		this.setCenterMc(this.$mcs[index-1]);
	}
	private function controlFun(){
		var xl:Number=this["root"].$centerX-this["root"].$centerMc._x;
		
		if(Math.abs(xl)>0.5){
			this["root"].checkAndSetMcArr();
			var index:Number=this["root"].findIndex(this["root"].$centerMc);
			if(index<0){
				this["root"].Say("Error on find center-movieclip");
				return;
			}else{
				this["root"].$centerMc._x+=xl/this["root"].$step;
				this["root"].$centerMc._y=this["root"].$centerY;
				var xl2:Number=this["root"].$centerX-this["root"].$centerMc._x;
				if(Math.abs(xl2)<=0.5){
					this["root"].broadcastMessage(VistaEFC2.VM_ONFOUCES,this["root"].$centerMc);
				}
				var cx=Math.abs(this["root"].$centerX-this["root"].$centerMc._x)/this["root"].$scaleLine;
				if(cx>1){
					cx=1;
				}
				cx=1-cx;
				cx=Math.pow(cx,this["root"].$blance);
				this["root"].$centerMc._xscale=this["root"].$centerMc._yscale=this["root"].$minScale+cx*(this["root"].$maxScale-this["root"].$minScale);
				this["root"].$centerMc._alpha=this["root"].$minAlpha+cx*(this["root"].$maxAlpha-this["root"].$minAlpha);
				
				for(var i=index-1;i>-1;i--){
					var xll:Number=Math.abs(this["root"].$centerX-this["root"].$mcs[i]._x)/this["root"].$scaleLine;
					if(xll>1){
						xll=1;
					}
					xll=1-xll;
					cx=Math.pow(xll,this["root"].$blance);
					this["root"].$mcs[i]._xscale=this["root"].$mcs[i]._yscale=this["root"].$minScale+xll*(this["root"].$maxScale-this["root"].$minScale);
					this["root"].$mcs[i]._x=this["root"].$mcs[i+1]._x-this["root"].$xoffsetN-(this["root"].$mcs[i+1]._width)/2-(this["root"].$mcs[i]._width)/2;
					this["root"].$mcs[i]._alpha=this["root"].$minAlpha+cx*(this["root"].$maxAlpha-this["root"].$minAlpha);
					this["root"].$mcs[i]._y=this["root"].$centerY;
				}
				
				for(var i=index+1;i<this["root"].$mcs.length;i++){
					var xll:Number=Math.abs(this["root"].$centerX-this["root"].$mcs[i]._x)/this["root"].$scaleLine;
					if(xll>1){
						xll=1;
					}
					xll=1-xll;
					cx=Math.pow(xll,this["root"].$blance);
					this["root"].$mcs[i]._xscale=this["root"].$mcs[i]._yscale=this["root"].$minScale+xll*(this["root"].$maxScale-this["root"].$minScale);
					this["root"].$mcs[i]._x=this["root"].$mcs[i-1]._x+this["root"].$xoffsetN+(this["root"].$mcs[i-1]._width)/2+(this["root"].$mcs[i]._width)/2;
					this["root"].$mcs[i]._alpha=this["root"].$minAlpha+cx*(this["root"].$maxAlpha-this["root"].$minAlpha);
					this["root"].$mcs[i]._y=this["root"].$centerY;
				}
			}
		}
	}
	private function findIndex(mc:MovieClip){
		for(var i:Number=0;i<this.$mcs.length;i++){
			if(this.$mcs[i]==mc){
				return i;
			}
		}
		return -1;
	}
	private function Say(o:Object){
		trace(o.toString());
	}
	private function getEffectMc(parent:MovieClip){
		if(parent==undefined){
			parent=_root;
		}
		this.$parent=parent;
		var EFMc:MovieClip=parent["_tophyVistaEFC"];
		if(EFMc==undefined){
			EFMc=parent.createEmptyMovieClip("_tophyVistaEFC",parent.getNextHighestDepth());
		}
		this.$EFfoot=EFMc;
		var index:Number=EFMc.getNextHighestDepth();
		if(index<=0){
			index=1;
		}
		this.$EFname="_VistaEFC2"+index.toString();
		var ef:MovieClip=EFMc.createEmptyMovieClip(this.$EFname,index);
		return ef;
	}
	
}